package com.jianyun.jyzs.update;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.jianyun.jyzs.R;


/**
 * Created by mlli on 2017/10/26.
 */

public class UpdateDialog extends AlertDialog{
    private Context mContext;
    private OnDialogButtonClickedListener mPromptButtonClickedListener;
    private String mVersion;
    private String mPositiveButton;
    private String mMessage;
    private boolean disableCancel;

    public static UpdateDialog getInstance(Context context,String version,String message){
        return new UpdateDialog(context,version,message);
    }

    protected UpdateDialog(Context context, String version, String message) {
        super(context);
        mContext = context;
        this.mVersion = version;
        mMessage = message;
    }

    @Override
    protected void onStart() {
        super.onStart();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = inflater.inflate(R.layout.gdb_update_dialog, null);
        TextView txtVersion = (TextView) view.findViewById(R.id.version_text);
        TextView txtViewMessage = (TextView) view.findViewById(R.id.popup_dialog_message);
        TextView txtViewOK = (TextView) view.findViewById(R.id.popup_dialog_button_ok);
        TextView txtViewCancel = (TextView) view.findViewById(R.id.popup_dialog_button_cancel);
        txtVersion.setText(mVersion);
        txtViewMessage.setText(mMessage);
        txtViewMessage.setMovementMethod(ScrollingMovementMethod.getInstance());
        if(disableCancel) txtViewCancel.setVisibility(View.GONE);
        txtViewOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPromptButtonClickedListener != null) {
                    mPromptButtonClickedListener.onPositiveButtonClicked();
                }
                dismiss();
            }
        });
        txtViewCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPromptButtonClickedListener != null) {
                    mPromptButtonClickedListener.onNegativeButtonClicked();
                }
                dismiss();
            }
        });
        setContentView(view);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = gePopupWidth();
        layoutParams.height = (int) (gePopupWidth()*1.2);
        getWindow().setAttributes(layoutParams);

    }
    public UpdateDialog setPromptButtonClickedListener(OnDialogButtonClickedListener buttonClickedListener) {
        this.mPromptButtonClickedListener = buttonClickedListener;
        return this;
    }
    private int gePopupWidth() {
        int distanceToBorder = (int) mContext.getResources().getDimension(R.dimen.rce_dimen_size_40);
        return getScreenWidth() - 2 * (distanceToBorder);
    }
    private int getScreenWidth() {
        return ((WindowManager) (mContext.getSystemService(Context.WINDOW_SERVICE))).getDefaultDisplay().getWidth();
    }

    public void disableCancel() {
        disableCancel= true;
        this.setCancelable(false);
    }

    public interface OnDialogButtonClickedListener {
        void onPositiveButtonClicked();
        void onNegativeButtonClicked();
    }
}
